Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports Csla
Imports Csla.Data
Imports Csla.Validation

<Serializable()> _
Public Class rw_divisionChild
    Inherits Csla.BusinessBase(Of rw_divisionChild)

#Region " Business Properties and Methods "

    ' declare members
    Private _divisionId As Integer = 0
    Private _divisionName As String = String.Empty
    Private _divisionDescription As String = String.Empty

    ''' <summary>
    ''' Get the DivisionId of the current object.
    ''' </summary>
    ''' <value>A Integer that represents the DivisionId of the current object.</value>
    ''' <returns>Integer representing the return value.</returns>
    ''' <remarks></remarks>
    <System.ComponentModel.DataObjectField(True, True)> _
    Public ReadOnly Property DivisionId() As Integer
        Get
            CanReadProperty("DivisionId", True)
            Return _divisionId
        End Get
    End Property

    ''' <summary>
    ''' Get or Set the DivisionName of the current object.
    ''' </summary>
    ''' <value>A String that represents the DivisionName of the current object.</value>
    ''' <returns>String representing the return value.</returns>
    ''' <remarks></remarks>
    Public Property DivisionName() As String
        Get
            CanReadProperty("DivisionName", True)
            Return _divisionName
        End Get
        Set(ByVal value As String)
            CanWriteProperty("DivisionName", True)
            If value Is Nothing Then value = String.Empty
            If Not _divisionName.Equals(value) Then
                _divisionName = value
                PropertyHasChanged("DivisionName")
            End If
        End Set
    End Property

    ''' <summary>
    ''' Get or Set the DivisionDescription of the current object.
    ''' </summary>
    ''' <value>A String that represents the DivisionDescription of the current object.</value>
    ''' <returns>String representing the return value.</returns>
    ''' <remarks></remarks>
    Public Property DivisionDescription() As String
        Get
            CanReadProperty("DivisionDescription", True)
            Return _divisionDescription
        End Get
        Set(ByVal value As String)
            CanWriteProperty("DivisionDescription", True)
            If value Is Nothing Then value = String.Empty
            If Not _divisionDescription.Equals(value) Then
                _divisionDescription = value
                PropertyHasChanged("DivisionDescription")
            End If
        End Set
    End Property

    ''' <summary>
    ''' Get the unique id for the current object.
    ''' </summary>
    ''' <returns>A value that is the unique id of the object.</returns>
    ''' <remarks></remarks>
    Protected Overrides Function GetIdValue() As Object
        Return _divisionId
    End Function

#End Region ' Business Properties and Methods

#Region " Validation Rules "

    ''' <summary>
    ''' Method that allows the addition of custom business rules.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub AddCustomRules()
        'todo: add custom/non-generated rules here...
    End Sub

    ''' <summary>
    ''' Method that allows the addition of shared custom business rules.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub AddSharedCustomRules()
        'todo: add custom/non-generated shared rules here...
    End Sub

    ''' <summary>
    ''' Method used to instigate the addition of all Business rules both Custom and Common.
    ''' </summary>
    ''' <remarks></remarks>
    Protected Overrides Sub AddInstanceBusinessRules()
        AddCustomRules()
        MyBase.AddBusinessRules()
    End Sub

    ''' <summary>
    ''' Method that allows the addition of shared common business rules.
    ''' </summary>
    ''' <remarks>These rules represent the database defaults that can be generated.</remarks>
    Private Sub AddSharedCommonRules()
        ' DivisionName rules
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringRequired, "DivisionName")
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringMaxLength, New Csla.Validation.CommonRules.MaxLengthRuleArgs("DivisionName", 50))
        ' DivisionDescription rules
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringRequired, "DivisionDescription")
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringMaxLength, New Csla.Validation.CommonRules.MaxLengthRuleArgs("DivisionDescription", 4000))
    End Sub

    ''' <summary>
    ''' Method used to instigate the addition of all Business rules both Custom and Common.
    ''' </summary>
    ''' <remarks></remarks>
    Protected Overrides Sub AddBusinessRules()
        AddSharedCommonRules()
        AddSharedCustomRules()
        MyBase.AddBusinessRules()
    End Sub

#End Region ' Validation Rules

#Region " Authorization Rules "

    Protected Overrides Sub AddAuthorizationRules()
        'TODO: Define authorization rules in rw_divisionChild
        'AuthorizationRules.AllowRead("DivisionId", "rw_divisionChildReadGroup")
        'AuthorizationRules.AllowRead("DivisionName", "rw_divisionChildReadGroup")
        'AuthorizationRules.AllowRead("DivisionDescription", "rw_divisionChildReadGroup")

        'AuthorizationRules.AllowWrite("DivisionName", "rw_divisionChildWriteGroup")
        'AuthorizationRules.AllowWrite("DivisionDescription", "rw_divisionChildWriteGroup")
    End Sub

#End Region ' Authorization Rules

#Region " Factory Methods "

    Friend Shared Function NewDivisionChild() As rw_divisionChild
        Return New rw_divisionChild()
    End Function

    Friend Shared Function GetDivisionChild(ByVal dr As SafeDataReader) As rw_divisionChild
        Return New rw_divisionChild(dr)
    End Function

    Private Sub New()

        ValidationRules.CheckRules()
        MarkAsChild()
    End Sub

    Private Sub New(ByVal dr As SafeDataReader)
        MarkAsChild()
        Fetch(dr)
    End Sub

#End Region ' Factory Methods

#Region " Data Access "

#Region " Data Access - Fetch "

    Private Sub Fetch(ByVal dr As SafeDataReader)
        FetchObject(dr)
        MarkOld()
        ValidationRules.CheckRules()

        'load child object(s)
        FetchChildren(dr)
    End Sub

    Private Sub FetchObject(ByVal dr As SafeDataReader)
        _divisionId = dr.GetInt32("divisionId")
        _divisionName = dr.GetString("divisionName")
        _divisionDescription = dr.GetString("divisionDescription")
    End Sub

    Private Sub FetchChildren(ByVal dr As SafeDataReader)
    End Sub

#End Region ' Data Access - Fetch

#Region " Data Access - Insert "

    Friend Sub Insert(ByVal cn As SqlConnection, ByVal parent As rw_LeagueRoot)
        If Not IsDirty Then Return
        ExecuteInsert(cn, parent)
        MarkOld()

        UpdateChildren(cn)
    End Sub

    Private Sub ExecuteInsert(ByVal cn As SqlConnection, ByVal parent As rw_LeagueRoot)
        Using cm As SqlCommand = cn.CreateCommand()
            cm.CommandType = CommandType.StoredProcedure
            cm.CommandText = "sp_insDivisionInfo"
            AddInsertParameters(cm, parent)
            cm.ExecuteNonQuery()

            _divisionId = CType(cm.Parameters("@new_divisionId").Value, Integer)
        End Using
    End Sub

    Private Sub AddInsertParameters(ByVal cm As SqlCommand, ByVal parent As rw_LeagueRoot)
        'todo: If parent use identity key, fix fk member with value from parent
        cm.Parameters.AddWithValue("@divisionName", _divisionName)
        cm.Parameters.AddWithValue("@divisionDescription", _divisionDescription)
        cm.Parameters.AddWithValue("@new_divisionId", _divisionId)
        cm.Parameters("@new_divisionId").Direction = ParameterDirection.Output
    End Sub

#End Region ' Data Access - Insert

#Region " Data Access - Update "

    Friend Sub Update(ByVal cn As SqlConnection, ByVal parent As rw_LeagueRoot)
        If Not IsDirty Then Return
        If MyBase.IsDirty Then
            ExecuteUpdate(cn, parent)
            MarkOld()
        End If

        UpdateChildren(cn)
    End Sub

    Private Sub ExecuteUpdate(ByVal cn As SqlConnection, ByVal parent As rw_LeagueRoot)

        Using cm As SqlCommand = cn.CreateCommand()
            cm.CommandType = CommandType.StoredProcedure
            cm.CommandText = "sp_updDivisionInfo"
            AddUpdateParameters(cm, parent)
            cm.ExecuteNonQuery()

        End Using
    End Sub

    Private Sub AddUpdateParameters(ByVal cm As SqlCommand, ByVal parent As rw_LeagueRoot)
        cm.Parameters.AddWithValue("@divisionId", _divisionId)
        cm.Parameters.AddWithValue("@divisionName", _divisionName)
        cm.Parameters.AddWithValue("@divisionDescription", _divisionDescription)
    End Sub

    Private Sub UpdateChildren(ByVal cn As SqlConnection)
    End Sub

#End Region ' Data Access - Update

#Region " Data Access - Delete "

    Friend Sub DeleteSelf(ByVal cn As SqlConnection)
        If Not IsDirty Then Return
        If IsNew Then Return
        ExecuteDelete(cn)
        MarkNew()
    End Sub

    Private Sub ExecuteDelete(ByVal cn As SqlConnection)
        Using cm As SqlCommand = cn.CreateCommand()
            cm.CommandType = CommandType.StoredProcedure
            cm.CommandText = "sp_delDivisionInfo"
            cm.Parameters.AddWithValue("@divisionId", _divisionId)

            cm.ExecuteNonQuery()
        End Using
    End Sub

#End Region ' Data Access - Delete

#End Region ' Data Access


End Class
